# -*- coding:UTF-8 -*-
#! /usr/bin/python

from tkinter import *
from random import randint
import sys

class grids(object):
    def __init__(self,master = None,height = 16,width = 24,offset = 10,gridWidth =50,bg = "grey"):
        self.master = master
        self.height = height
        self.width = width
        self.offset = offset
        self.gridWidth = gridWidth
        self.bg = bg
        self.canvas = Canvas(master,width = self.width * self.gridWidth + 2 * self.offset,height = self.height * self.gridWidth + 2 * self.offset,bg = self.bg)
        self.canvas.pack()
    def draw(self,pos,color):
        x = pos[0] * self.gridWidth + self.offset
        y = pos[1] * self.gridWidth + self.offset
        self.canvas.create_rectangle(x,y,x + self.gridWidth,y + self.gridWidth,fill = color,outline = self.bg)

class food(object):
    def __init__(self,grids,color = 'red'):
        self.grids = grids
        self.color = color
    def set_pos(self):
        x = randint(0,self.grids.width - 1)
        y = randint(0,self.grids.height - 1)
        self.pos = (x,y)
        self.grids.draw(self.pos,self.color)

class snake(object):
    def __init__(self,grids,color = 'black'):
        self.grids = grids
        self.color = color
        x = self.grids.width // 2
        y = self.grids.height // 2
        self.body = [(x-1,y),(x,y),(x+1,y)]
        self.dir = 'Left'
        for i in self.body:
            self.grids.draw(i,self.color)
    def add(self):
        head =self.body[0]
        if self.dir == 'Left':
            new = ((head[0]-1)%self.grids.width,(head[1])%self.grids.height)
        elif self.dir == 'Right':
            new = ((head[0]+1)%self.grids.width,(head[1])%self.grids.height)
        elif self.dir == 'Up':
            new = ((head[0])%self.grids.width,(head[1]-1)%self.grids.height)
        elif self.dir == 'Down':
            new = ((head[0])%self.grids.width,(head[1]+1)%self.grids.height)
        if new != self.body[1]:
            self.body.insert(0,new)
            self.grids.draw(new,self.color)
    def move(self):
        length = len(self.body)
        self.add()
        if length < len(self.body):
            pop = self.body.pop()
            self.grids.draw(pop,self.grids.bg)
    def cut(self):
        length = len(self.body)
        self.add()
        if length < len(self.body):
            pop = self.body.pop()
            self.grids.draw(pop,self.grids.bg)
            pop = self.body.pop()
            self.grids.draw(pop,self.grids.bg)
    def change_direction(self,dir):
        self.dir = dir        

class snakeGame(Frame):
    def __init__(self,master = None):
        Frame.__init__(self,master)
        self.grids = grids(master)
        self.snake = snake(self.grids)
        self.food = food(self.grids)
        self.food.set_pos()
        self.speed = 300
        self.gameOver = False
        self.score = 0
        self.status = 'run'
        self.bind_all("<KeyRelease>", self.key_release)
    def run(self):
        if self.status == 'run':
            if self.snake.body[0] == self.food.pos:
                self.snake.add()
                self.food.set_pos()
                while self.food.pos in self.snake.body:
                    self.food.set_pos()
            else:
                self.snake.move()
        else:
            Message = tkMessageBox.showinfo('Game Over')
        self.after(self.speed,self.run)
    def key_release(self, event):
        key = event.keysym
        key_dict = {"Up":"Down","Down":"Up","Left":"Right","Right":"Left"}
        if not key == key_dict[self.snake.dir]:
            self.snake.change_direction(key)
            self.snake.move()
        elif key == 'p':
            self.snake.status = 'stop'

game = snakeGame()
game.run()
game.mainloop()
